package it.softphone.cmc.event.client;

import com.google.gwt.appengine.channel.client.Channel;
import com.google.gwt.appengine.channel.client.ChannelFactory.ChannelCreatedCallback;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ScriptElement;
import com.google.gwt.user.client.Timer;

/**
 * 
 * @author bsorrentino
 *
 */
public class CrossDomainChannelFactory {
	  private static final String CHANNEL_SRC = "/_ah/channel/jsapi";
	  private static Channel channel;

	  /**
	   * Creates a {@link Channel} with the given client ID, passing it to the given
	   * {@link ChannelCreatedCallback}.
	   *
	   * <p>
	   * If a Channel has already been created, this will immediately be passed to
	   * the callback, so only one Channel will ever be created by this method.
	   * </p>
	   */
	  public static void createChannel( final String domain,  final String clientId, final ChannelCreatedCallback channelCreatedCallback) {
	    if (channel == null) {
	      ScriptElement script = Document.get().createScriptElement();
	      script.setSrc( domain.concat(CHANNEL_SRC));
	      Document.get().getElementsByTagName("head").getItem(0).appendChild(script);

	      new Timer() {
	        @Override
	        public void run() {
	          if (scriptLoaded()) {
	            channel = createChannelImpl(clientId);
	            channelCreatedCallback.onChannelCreated(channel);
	            this.cancel();
	          }
	        }
	      }.scheduleRepeating(100);
	    } else {
	      channelCreatedCallback.onChannelCreated(channel);
	    }
	  }

	  private static native boolean scriptLoaded() /*-{
	    return !!$wnd.goog && !!$wnd.goog.appengine && !!$wnd.goog.appengine.Channel;
	  }-*/;

	  private static final native Channel createChannelImpl(String clientId) /*-{
	    return new $wnd.goog.appengine.Channel(clientId);
	  }-*/;

}
